package str59_zapis_i_odczyt_obiektow_do_pliku;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {
	public static final String FILE_NAME = "data.dat";
	
	public static void main(String[] args) {
		Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
		Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
		carl.setSecretary(harry);
		Manager tony = new Manager("Tony Tester", 40000, 1990, 3, 15);
		tony.setSecretary(harry);
		
		Employee[] staff = new Employee[3];
		
		staff[0] = carl;
		staff[1] = harry;
		staff[2] = tony;
		
		try {
			FileOutputStream fos = new FileOutputStream(FILE_NAME);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(staff);
			oos.close();
			
			FileInputStream fis = new FileInputStream(FILE_NAME);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Employee[] newStaff = (Employee[]) ois.readObject();
			
			for (Employee employee : newStaff) {
				System.out.println(employee);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
